package markdown

import(
	"strings"
	"log"
)

func ParsePair(src string, mark string, pair string) string {
	var result string
	if !strings.Contains(src, mark) {
		return src
	}
	result += "<" + pair +">"
	result += strings.Replace(src, mark, "", 1)
	result +="</"+pair+">"
	log.Println(result)

	return result
}

func ParseLines(lines []string, mark string, pair string, outMark string) string{

	tags := map[string]string{"h1. ": "h1", "h2. ": "h2", "h3. ":"h3", "h4. ":"h4","h5. ":"h5", "h6. ":"h6","tag: ":"tag"}

	log.Println("ParseLines")
	var result string
	step := "init"
	for _, line := range lines{
		if strings.Contains(line, mark) {
			if step == "init"{
				step = "started"
			}else{
				step = "processing"
			}
		}else{
			if step == "processing" {
				step = "finished"
			}
		}
		
		//process everything
		if step == "started" {
			result += "<"+outMark+">"
		}


		if step == "finished" {
			result += "</"+outMark+">"
			//循环
			step = "init"
		}

		haveParseTag := false
		if step == "init" {
			for key, value := range tags {
				if strings.Contains(line, key){
					result += ParsePair(line, key, value)
					haveParseTag = true
					continue
				}
			}
			if !haveParseTag {
				result += ParsePair(line, mark, pair)
			}
		}else{
			result += ParsePair(line, mark, pair)
		}
		//log.Println(step +":"+result)
	}

	return result
}


